﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class PushMoneyManage_PushMoneyDetail : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["FCode"] = "300500101";
        if (!IsPostBack)
        {
            if (Request.Params["ID"] != null)
            {
                Init_ApplyMainTable();
            }
        }
    }
    #region 初始化主表信息
    private void Init_ApplyMainTable()
    {
        string id=Request.Params["ID"].ToString();
        Model.TiChenApply tichen = new Model.TiChenApply();
        tichen = new BLL.TiChenApply().GetModel(int.Parse(id));
        if(tichen!=null)
        {
            lbl_DJBH.Text = tichen.App_ID.ToString();
            lbl_applyman.Text = tichen.App_Code;
            lbl_applyamt.Text = tichen.App_Amt.ToString();
            lbl_tax.Text = tichen.App_Tax.ToString();
            CKB_Valid.Checked = tichen.App_Valid == 1 ? true : false;
            lbl_factAmt.Text = tichen.App_FactAmt.ToString();
            lbl_ValidMan.Text = tichen.App_ValidMan;
            lbl_ValidDate.Text = tichen.App_ValidDate.ToString();
            txtNote.Text = tichen.App_Note.ToString();
            CKB_ZF.Checked = tichen.App_IsDel == 1 ? true : false;
            lbl_zfMan.Text = tichen.App_DelMan;
            lbl_ZFdate.Text = tichen.App_DelDate.ToString();

            if (tichen.App_Valid == 1 || tichen.App_IsDel == 1)
            {
                btnDelete.Enabled = false;
                btnAudit.Enabled = false;
            }
        }
        
    }
    #endregion
     
    protected void btnAudit_Click(object sender, EventArgs e)
    {
        Model.TiChenApply ticheng = new BLL.TiChenApply().GetModel(int.Parse(Request.Params["ID"]));

        if (ticheng != null)
        {
            if (ticheng.App_Valid == 1)
            {
                BLL.Common.Alert(Page, "此单据已被审核，不允许再次操作！"); return;
            }
        }

        Model.V_VIPSET vipset = new BLL.V_VIPSET().GetModelByGKDM(ticheng.App_Code);

        Model.V_VIP_ACCOUNT account = new BLL.V_VIP_ACCOUNT().GetModel(vipset.DM);

        decimal balance = ticheng.App_FactAmt;
        if (account != null)
        {
            balance = (account.DQJE_1 + ticheng.App_FactAmt);
        }

        StringBuilder strSql = new StringBuilder("update TiChenApply set App_Valid=1,App_ValidMan='" + Share.UserValue(0) + "',App_ValidDate='" + DateTime.Now + "' where App_ID=" + Request.Params["ID"] + " and App_IsDel=0 and App_Valid=0");
        strSql.Append(" UPDATE BankRollDayToDayAccount SET [BDDA_SendFlag]=1 WHERE [BDDA_ID] IN (SELECT [BYZD5] FROM [TiChenMX] WHERE [TC_ID]=" + Request.Params["ID"] + ")");
        strSql.Append(" insert into BALANCEADJUST(BA_ID,BA_RID,BA_TYPE,BA_CREATER,BA_CREATEDATE,BA_FIELD1) values('" + BLL.Common.GetDM("300400102") + "',null,'010','" + Share.UserValue(0) + "','" + DateTime.Now + "',"+ Request.Params["ID"] +")");//插入一条调整单
        strSql.Append(" insert into BADETAIL(BAD_ID,BAD_DETAILCODE,BAD_CUSTOMERCODE,BAD_AMT,BAD_BALANCE) select'" + BLL.Common.GetDM("300400102") + "',1,'" + vipset.GKDM + "',App_FactAmt as BAD_AMT," + balance + " FROM TiChenApply WHERE App_ID=" + Request.Params["ID"]);//插入一条调整单明细
        if (new BLL.TiChenApply().AuditApply(strSql.ToString())>0)
        {

            BLL.Common.ADDLSH("300400102");
            Init_ApplyMainTable();
            BLL.Common.Alert(this.Page, "审核成功，同时生成的一张相关的结余款调整单需要审核");

        }
        else
        {
            BLL.Common.Alert(this.Page, "审核失败");
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {//strSql.Append("UPDATE BankRollDayToDayAccount SET [BDDA_SendFlag]=1 WHERE [BDDA_ID] IN (SELECT [BYZD5] FROM [TiChenMX] WHERE [TC_ID]=" + Request.Params["ID"] + ")");
        string sql = "update TiChenApply set App_DelMan='" + Share.UserValue(0) + "' ,App_IsDel=1 ,App_DelDate='" + DateTime.Now + "' where App_ID=" + Request.Params["ID"] + " and App_Valid=0";
         sql = sql + " UPDATE BankRollDayToDayAccount SET [BDDA_SendFlag]=0 WHERE [BDDA_ID] IN (SELECT [BYZD5] FROM [TiChenMX] WHERE [TC_ID]=" + Request.Params["ID"]+")";
        new BLL.TiChenApply().DeleteApply(sql);
        Init_ApplyMainTable();

    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("PushMoney.aspx");
    }
    protected void btnAddDetail_Click(object sender, EventArgs e)
    {
    }
}
